package client.gui.lobby;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.util.Deque;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import lib.Commands;
import client.TCPClientListener;

public class ChatPanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4673474228920844484L;
	public TCPClientListener listener=null;
	private JTextArea ChatText = null;
	private JTextField InputBox = null;
	private JButton SendButton = null;
	private JScrollPane ChatTextScroller = null;
	private Lobby parent=null;
	private JTextArea User = null;
	/**
	 * This method initializes
	 * 
	 * @param client
	 * 
	 */
	public ChatPanel(Lobby l) {
		super();
		this.parent = l;
		try {
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initialize();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		gridBagConstraints7.fill = GridBagConstraints.BOTH;
		gridBagConstraints7.weighty = 1.0;
		gridBagConstraints7.weightx = 1.0;
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.fill = GridBagConstraints.BOTH;
		gridBagConstraints4.gridy = 0;
		gridBagConstraints4.weightx = 1.0;
		gridBagConstraints4.weighty = 1.0;
		gridBagConstraints4.gridx = 1;
		GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
		gridBagConstraints21.fill = GridBagConstraints.BOTH;
		gridBagConstraints21.gridy = 0;
		gridBagConstraints21.weightx = 1.0;
		gridBagConstraints21.weighty = 1.0;
		gridBagConstraints21.gridx = 2;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.gridx = 3;
		gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints11.gridy = 0;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.fill = GridBagConstraints.BOTH;
		gridBagConstraints2.gridwidth = 1;
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.gridy = 0;
		gridBagConstraints2.ipadx = 175;
		gridBagConstraints2.ipady = -34;
		gridBagConstraints2.weightx = 1.0;
		gridBagConstraints2.weighty = 1.0;
		gridBagConstraints2.insets = new Insets(15, 15, 0, 18);
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.insets = new Insets(1, 7, 19, 20);
		gridBagConstraints1.gridy = 1;
		gridBagConstraints1.ipadx = 10;
		gridBagConstraints1.ipady = -6;
		gridBagConstraints1.gridx = 1;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = GridBagConstraints.VERTICAL;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.ipadx = 207;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.insets = new Insets(1, 15, 19, 7);
		this.setLayout(new GridBagLayout());
		this.setSize(new Dimension(412, 133));
		this.setName("ChatPanel");
		this.add(getInputBox(), gridBagConstraints);
		this.add(getSendButton(), gridBagConstraints1);
		this.add(getChatTextScroller(), gridBagConstraints2);
		this.add(getUser(), gridBagConstraints7);
	}

	/**
	 * This method initializes ChatText
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getChatText() {
		if (ChatText == null) {
			ChatText = new JTextArea(7, 10);
			ChatText.setBounds(new Rectangle(60, 21, 226, 121));
			ChatText.setEditable(false);
			ChatText.setLineWrap(true);
			ChatText.setText("Welcome to Project Theta");
			ChatText.setWrapStyleWord(true);
		}
		return ChatText;
	}

	/**
	 * This method initializes ChatTextScroller
	 * 
	 * @return javax.swing.JScrollBar
	 */
	private JScrollPane getChatTextScroller() {
		if (ChatTextScroller == null) {
			ChatTextScroller = new JScrollPane(getChatText(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			ChatTextScroller.setAutoscrolls(true);
		}
		return ChatTextScroller;
	}

	/**
	 * This method initializes InputBox
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getInputBox() {
		if (InputBox == null) {
			InputBox = new JTextField();
			InputBox.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) {
					if (e.getKeyChar() == '\n')
						send(Commands.SEND, InputBox.getText());
				}
			});
		}
		return InputBox;
	}

	/**
	 * This method initializes SendButton
	 * 
	 * @return javax.swing.SendButton
	 */
	private JButton getSendButton() {
		if (SendButton == null) {
			SendButton = new JButton();
			SendButton.setText("Send:");
			SendButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					send(Commands.SEND, InputBox.getText());
				}
			});
		}
		return SendButton;
	}

	public void send(Commands cmd, String msg) {
		if (msg.indexOf("/") == 0)
			parent.listener.send(msg.substring(1));
		else
			parent.send(cmd, msg);
		InputBox.setText("");
	}

	public void displayMessage(String msg) {
		ChatText.append("\n" + msg);
		ChatText.setCaretPosition(ChatText.getText().length());
	}





	/**
	 * This method initializes User	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getUser() {
		if (User == null) {
			User = new JTextArea();
			User.setEditable(false);
		}
		return User;
	}
	public void updateUserList(Deque<String> msg){
		User.setText("");
		for(String s:msg){
			User.append(s+"\n");
		}
		repaint();
	}

} // @jve:decl-index=0:visual-constraint="86,120"
